import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router';

import { getToken } from '@/utils/token.js'
import NPrrogress from 'nprogress';
import 'nprogress/nprogress.css';

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'root',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/Home/index.vue'),
      meta: {
        isShowNav: true,
        login: false,
      }
    },
    {
      path: '/recommend',
      name: 'recommend',
      component: () => import('@/views/Recommend/index.vue'),
      meta: {
        isShowNav: true,
        login: true,
        title: '种草推荐',
      }
    },
    {
      path: '/articleDetail',
      name: 'articleDetail',
      component: () => import('@/views/Recommend/ArticleDetail.vue'),
      meta: {
        login: true,
        title: '种草详情',
      }
    },
    {
      path: '/myArticle',
      name: 'myArticle',
      component: () => import('@/views/Recommend/MyArticle.vue'),
      meta: {
        login: true,
        title: '我的文章',
      }
    },
    {
      path: '/addArticle',
      name: 'addArticle',
      component: () => import('@/views/Recommend/addArticle.vue'),
      meta: {
        login: true,
        title: '新增种草',
      }
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('@/views/Cart/index.vue'),
      meta: {
        isShowNav: true,
        title: '购物车',
      }
    },
    {
      path: '/goodDetails/:id',
      name: 'goodDetails',
      component: () => import('@/views/goodDetails/index.vue'),
      meta: {
        isShowNav: false,
        title: '商品详情',
      }
    },
    {
      path: '/address',
      name: 'address',
      component: () => import('@/views/Address/index.vue'),
      meta: {
        login: true,
        isShowNav: false,
        title: '收获地址',
      }
    },
    {
      path: '/addAddress',
      name: 'addAddress',
      component: () => import('@/views/Address/addAddress.vue'),
      meta: {
        login: true,
        isShowNav: false,
        title: '添加地址',
      }
    },
    {
      path: '/createOrder',
      name: 'createOrder',
      component: () => import('@/views/Order/CreateOrder.vue'),
      meta: {
        title: '生成订单',
        login: true,
      },
    },
    {
      path: '/order',
      name: 'order',
      component: () => import('@/views/Order/index.vue'),
      meta: {
        title: '我的订单',
        login: true,
      },
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('@/views/User/index.vue'),
      meta: {
        isShowNav: true,
        title: '我的',
      },
    },
    {
      path: '/myInfo',
      name: 'myInfo',
      component: () => import('@/views/User/MyInfo.vue'),
      meta: {
        isShowNav: false,
        title: '个人信息',
        login: true,
      },
    },
    {
      path: '/collect',
      name: 'collect',
      component: () => import('@/views/collect/index.vue'),
      meta: {
        isShowNav: false,
        title: '我的收藏',
        login: true,
      },
    },
    {
      path: '/praise',
      name: 'praise',
      component: () => import('@/views/Praise/index.vue'),
      meta: {
        isShowNav: false,
        title: '我的点赞',
        login: true,
      },
    },
    {
      path: '/changePwd',
      name: 'changePwd',
      component: () => import('@/views/User/ChangePwd.vue'),
      meta: {
        isShowNav: false,
        title: '修改密码',
      },
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/User/Login.vue'),
      meta: {
        title: '登录',
      },
    },
    // 访问没有的路由直接跳往首页
    {
      path: '/:toHome*',
      name: 'toHome',
      redirect: '/home',
    },
  ]
});
NPrrogress.configure({ showSpinner: false });

router.beforeEach((to, from, next) => {
  NPrrogress.start();
  let token = getToken()
  if (token) {
    if (to.path === './login') {
      return next({ path: from.path })
    }
  } else {
    // 判断是否为必须登录，如果是必须登录就跳转到登录页
    if (to.meta.login == true) {
      return next({ path: './login' })
    }
  }
  return next();
});

router.afterEach(() => {
  NPrrogress.done();
})
export default router
